package com.animator.demo

import android.animation.Animator
import android.animation.ObjectAnimator
import android.graphics.Path
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import android.view.animation.PathInterpolator
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity

/**
 *Android中属性动画的应用.
 */
class MainActivity : AppCompatActivity() {
    companion object {
        private const val TAG = "MainActivity"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        testPathAnimator()
    }

    private fun testPathAnimator() {
        val l = findViewById<View>(R.id.root_view) as FrameLayout
        val imageView = ImageView(this)
        imageView.setImageResource(R.drawable.dot)
        val param = FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
        )
        l.addView(imageView, param)
        val path = Path().apply {
            moveTo(200f, 200f)
            lineTo(200F * 3, 200F)
            lineTo(200F * 3, 200F * 5)
            lineTo(200F * 1, 200F * 5)
            lineTo(200F * 1, 200F * 2)
        }
        //path.quadTo(800f, 200f, 800f, 800f)
        //val pathInterpolator = PathInterpolator(0.33f, 0f, 0.12f, 1f)
        //val pathInterpolator = PathInterpolator(path)
        val moveAnimator = ObjectAnimator.ofFloat(imageView, "x", "y", path)
        moveAnimator.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {
                Log.i(TAG, "onAnimationStart: ")
            }

            override fun onAnimationEnd(animation: Animator) {
                l.removeView(imageView)
            }

            override fun onAnimationCancel(animation: Animator) {
                l.removeView(imageView)
            }

            override fun onAnimationRepeat(animation: Animator) {

            }
        })
        moveAnimator.addUpdateListener {
            //Log.i(TAG, "addUpdateListener: ${it.values[0].propertyName}:${it.values[1].propertyName}")
            //Log.i(TAG, "addUpdateListener: ${it.animatedValue}")
            Log.i(
                TAG,
                "addUpdateListener:(x:${it.getAnimatedValue(it.values[0].propertyName)},y:${
                    it.getAnimatedValue(it.values[1].propertyName)
                }) "
            )
            //Log.i(TAG, "addUpdateListener: ${it.values[0].propertyName}:${it.values[0].setObjectValues()}")
        }
        /*moveAnimator.addUpdateListener(object : ValueAnimator.AnimatorUpdateListener {
            override fun onAnimationUpdate(animation: ValueAnimator) {
                //ValueAnimator
                animation.animatedValue
            }
        })*/
        //moveAnimator.interpolator = pathInterpolator
        moveAnimator.interpolator = LinearInterpolator()
        moveAnimator.duration = 5 * 1000
        moveAnimator.start()
    }
}